﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    public class StringHelper
    {

        public static bool VeriFy(string val, string regstr)
        {
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regstr);
            return reg.IsMatch(val);
        }

        /// <summary>
        /// 如果字符串为空或NULL，抛出异常
        /// </summary>
        /// <param name="Value">要判断的字符串</param>
        /// <param name="Describe">字符串描述</param>
        static void IsNullThrow(string Value, string Describe)
        {
            if (string.IsNullOrEmpty(Value))
            {
                throw new Exception(Describe + "不能为空！");
            }
        }
        /// <summary>
        /// 判断字符串是否能转换成整型（int）
        /// </summary>
        /// <param name="Value">要判断的字符串</param>
        /// <returns>True/False</returns>
        public static bool IsConvertToInt(string Value)
        {
            int result;
            return int.TryParse(Value, out result);
        }
        /// <summary>
        /// 判断字符串是否能转换成整型（int）
        /// </summary>
        /// <param name="Value">要判断的字符串</param>
        /// <returns>True/False</returns>
        public static bool IsConvertToFloat(string Value)
        {
            float result;
            return float.TryParse(Value, out result);
        }
        /// <summary>
        /// 判断字符串是否能转换成日期类型
        /// </summary>
        /// <param name="Value">要判断的字符串</param>
        /// <returns>True/False</returns>
        public static bool IsConvertToDateTime(string Value)
        {
            if (string.IsNullOrWhiteSpace(Value))
                return false;
            DateTime result;
            return DateTime.TryParse(Value, out result);
        }
        /// <summary>
        /// 限制字符串长度,当超过指定长度时,截取并在字符串尾部加上指定字符串
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <param name="length">长度</param>
        /// <param name="Tailer">尾随字符窜</param>
        /// <returns>string</returns>
        public static string LimitString(string Str, int length, string Tailer)
        {
            if (Str.Length > length)
            {
                return Str.Substring(0, length) + Tailer;
            }
            else
            {
                return Str;
            }
        }
        /// <summary>
        /// 截取指定长度的字符串，当超过指定长度时,截取并在字符串尾部加上指定字符串（支持中英文混合截取）
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <param name="length">截取长度</param>
        /// <param name="Tailer">尾随字符窜</param>
        /// <returns>string</returns>
        public static string LimitStr(string str, int n, string Tailer)
        {
            ///
            ///格式化字符串长度，超出部分显示省略号,区分汉字跟字母。汉字2个字节，字母数字一个字节
            ///
            string temp = string.Empty;
            if (System.Text.Encoding.Default.GetByteCount(str) <= n)//如果长度比需要的长度n小,返回原字符串
            {
                return str;
            }
            else
            {
                int t = 0;
                char[] q = str.ToCharArray();
                for (int i = 0; i < q.Length && t < n; i++)
                {
                    if ((int)q[i] >= 0x4e00 && (int)q[i] <= 0x9fa5)//是否汉字
                    {
                        if (t + 2 > n)
                            break;
                        temp += q[i];
                        t += 2;
                    }
                    else
                    {
                        temp += q[i];
                        t++;
                    }
                }
                return (temp + Tailer);
            }

        }
        /// <summary>
        ///  用指定字符把字符串拆分成数组
        /// </summary>
        /// <param name="mystring">被拆分的字符串</param>
        /// <param name="mychar">拆分用的字符</param>
        /// <returns>string[]数组</returns>
        public static string[] StringSplit(string mystring, char mychar)
        {
            string[] myarray = mystring.Split(mychar);
            return myarray;
        }
        /// <summary>
        /// 用指定的分隔符将对应的数组分割成字符串并返回。
        /// </summary>
        /// <param name="StrArr">字符串数组</param>
        /// <param name="Delimiter">分隔符</param>
        /// <returns></returns>
        public static string ArrayToString(string[] StrArr, string Delimiter)
        {
            if (StrArr.Length == 0)
                return "";
            string ResultStr = "";
            foreach (string CurrentStr in StrArr)
            {
                if (CurrentStr.Trim() != "")
                {
                    ResultStr += Delimiter;
                    ResultStr += "'" + CurrentStr + "'";
                }
            }
            if (ResultStr.Length > 0)
            {
                ResultStr = ResultStr.Substring(Delimiter.Length, ResultStr.Length - Delimiter.Length);
            }
            return ResultStr;
        }
        /// <summary>
        ///  截取日期为YYYY-MM-DD格式
        /// </summary>
        /// <param name="ShortTime">完整日期字符串0000-00-00 00:00:00</param>
        /// <returns></returns>
        public static string getDateTimeShort(string ShortTime)
        {
            if (ShortTime != "")
            {
                return ShortTime.Substring(0, ShortTime.IndexOf(' '));
            }
            else
            {
                return "";
            }
        }

        public static string ChineseCaps(string ChineseStr)
        {
            string Capstr = "";

            byte[] ZW = new byte[2];
            long ChineseStr_int;
            string CharStr, ChinaStr = "";
            for (int i = 0; i <= ChineseStr.Length - 1; i++)
            {
                CharStr = ChineseStr.Substring(i, 1).ToString();
                ZW = System.Text.Encoding.Default.GetBytes(CharStr);
                // 得到汉字符的字节数组 
                if (ZW.Length == 2)
                {
                    int i1 = (short)(ZW[0]);
                    int i2 = (short)(ZW[1]);
                    ChineseStr_int = i1 * 256 + i2;


                    if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
                    {
                        ChinaStr = "A";
                    }
                    else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
                    {
                        ChinaStr = "B";
                    }
                    else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
                    {
                        ChinaStr = "C";

                    }
                    else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
                    {
                        ChinaStr = "D";
                    }
                    else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
                    {
                        ChinaStr = "E";
                    }
                    else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
                    {
                        ChinaStr = "F";
                    }
                    else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
                    {
                        ChinaStr = "G";
                    }
                    else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
                    {

                        ChinaStr = "H";
                    }

                    else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
                    {
                        ChinaStr = "J";
                    }
                    else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
                    {
                        ChinaStr = "K";
                    }
                    else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
                    {
                        ChinaStr = "L";
                    }
                    else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
                    {
                        ChinaStr = "M";
                    }

                    else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
                    {
                        ChinaStr = "N";

                    }
                    else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
                    {
                        ChinaStr = "O";
                    }
                    else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
                    {
                        ChinaStr = "P";

                    }
                    else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
                    {
                        ChinaStr = "Q";

                    }
                    else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
                    {
                        ChinaStr = "R";
                    }
                    else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
                    {
                        ChinaStr = "S";
                    }
                    else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
                    {
                        ChinaStr = "T";
                    }
                    else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
                    {
                        ChinaStr = "W";
                    }
                    else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
                    {
                        ChinaStr = "X";
                    }
                    else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
                    {
                        ChinaStr = "Y";
                    }
                    else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
                    {
                        ChinaStr = "Z";
                    }
                }
                else
                {
                    Capstr = ChineseStr;
                    break;
                }

                Capstr = Capstr + ChinaStr;
            }
            return Capstr;
        }
        //获取汉字首字母
        public static string GetChineseSpell(string strText)
        {
            string myStr = "";
            if (!string.IsNullOrEmpty(strText))
            {
                int len = strText.Length;
                for (int i = 0; i < len; i++)
                {
                    myStr += getSpell(strText.Substring(i, 1));
                }
            }
            return myStr;
        }
        private static string getSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else return cnChar;
        }
        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>        
        public static string ToSBC(string input)
        {
            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>
        public static string ToDBC(string input)
        {
            input = input.Replace('【', '[').Replace('】', ']');
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        /// <summary>
        ///计算百分比  191
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static string CountRate(int a, int b)
        {
            if (b == 0)
            {
                return "0%";
            }
            else
            {
                return (Math.Round((a / (float)b), 4) * 100).ToString() + "%";
            }
        }

        //验证格式
        public class vertify
        {
            /// <summary>
            /// 数字
            /// </summary>
            public static string iDecimal
            {
                get { return @"\d+(\.\d+)?"; }
            }
            public static string iInt
            {
                get { return @"\d+"; }
            }
            public static string iMail
            {
                get { return @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; }
            }
            public static string QSType
            {
                get { return @"\^QS\d{4}\ \d{4} \d{4}$"; }
            }
        }

        public static string ChinaToNumber(string str)
        {
            string res = str;
            switch (str)
            {
                case "一":
                    res = "1";
                    break;
                case "二":
                case "两":
                    res = "2";
                    break;
                case "三":
                    res = "3";
                    break;
                case "四":
                    res = "4";
                    break;
                case "五":
                    res = "5";
                    break;
                case "六":
                    res = "6";
                    break;
                case "七":
                    res = "7";
                    break;
                case "八":
                    res = "8";
                    break;
                case "九":
                    res = "9";
                    break;
            }
            return res;
        }

        public static string[] ToChinaDate(DateTime mydate)
        {
            string Date_Y = mydate.Year.ToString();
            string Date_M = mydate.Month.ToString(); ;
            string Date_D = mydate.Day.ToString(); ;

            string datey = "";
            string datem = "";
            string dated = "";
            for (int i = 0; i < Date_Y.Length; i++)
            {
                switch (Date_Y.Substring(i, 1))
                {
                    case "0":
                        datey = datey + "○";
                        break;
                    case "1":
                        datey = datey + "一";
                        break;
                    case "2":
                        datey = datey + "二";
                        break;
                    case "3":
                        datey = datey + "三";
                        break;
                    case "4":
                        datey = datey + "四";
                        break;
                    case "5":
                        datey = datey + "五";
                        break;
                    case "6":
                        datey = datey + "六";
                        break;
                    case "7":
                        datey = datey + "七";
                        break;
                    case "8":
                        datey = datey + "八";
                        break;
                    case "9":
                        datey = datey + "九";
                        break;
                }
            }
            if (Date_M.Length > 1)
            {
                switch (Date_M.Substring(0, 1))
                {
                    case "1":
                        datem = datem + "十";
                        break;
                    case "2":
                        datem = datem + "二十";
                        break;
                    case "3":
                        datem = datem + "三十";
                        break;
                }
            }
            switch (Date_M.Substring(Date_M.Length - 1, 1))
            {
                case "1":
                    datem = datem + "一";
                    break;
                case "2":
                    datem = datem + "二";
                    break;
                case "3":
                    datem = datem + "三";
                    break;
                case "4":
                    datem = datem + "四";
                    break;
                case "5":
                    datem = datem + "五";
                    break;
                case "6":
                    datem = datem + "六";
                    break;
                case "7":
                    datem = datem + "七";
                    break;
                case "8":
                    datem = datem + "八";
                    break;
                case "9":
                    datem = datem + "九";
                    break;
            }
            if (Date_D.Length > 1)
            {
                switch (Date_D.Substring(0, 1))
                {
                    case "1":
                        dated = dated + "十";
                        break;
                    case "2":
                        dated = dated + "二十";
                        break;
                    case "3":
                        dated = dated + "三十";
                        break;
                }
            }
            switch (Date_D.Substring(Date_D.Length - 1, 1))
            {
                case "1":
                    dated = dated + "一";
                    break;
                case "2":
                    dated = dated + "二";
                    break;
                case "3":
                    dated = dated + "三";
                    break;
                case "4":
                    dated = dated + "四";
                    break;
                case "5":
                    dated = dated + "五";
                    break;
                case "6":
                    dated = dated + "六";
                    break;
                case "7":
                    dated = dated + "七";
                    break;
                case "8":
                    dated = dated + "八";
                    break;
                case "9":
                    dated = dated + "九";
                    break;
            }

            string[] date1 = new string[3];
            date1[0] = datey;
            date1[1] = datem;
            date1[2] = dated;
            return date1;
        }
        /// <summary>
        /// 将一个日期格式的值转化为【xxxx年xx月xx日】格式的数据，若为null,则返回【 年 月 日】
        /// </summary>
        /// <param name="mydate"></param>
        /// <returns></returns>
        public static string ConventDateFormat(DateTime? mydate)
        {
            if (mydate == null)
                return "  年  月  日 ";
            //return mydate.Year.ToString() + "年" + mydate.Month.ToString() + "月" + mydate.Day.ToString() + "日";
            return mydate.Value.ToString("yyyy年MM月dd日");
        }
    }
}
